package asimo_without_encircling;

import robocode.ScannedRobotEvent;
import robocode.util.Utils;

import java.awt.geom.*;
import java.util.*;

public class Battle {
	
	 protected static void circularShooting(SimpleDrone MyRobot, ScannedRobotEvent e, double bulletPower){
	        //set bullet power
		 	bulletPower = Math.min(bulletPower, MyRobot.getEnergy());
	        //my current position
	        double myX = MyRobot.getX(), myY = MyRobot.getY();
	        
	        /*
	         * getHeadingRadians() returns the direction that the robot's body is facing, in radians. 
	         * The value returned will be between 0 and 2 * PI (is excluded). 
	         * 0 means North, PI / 2 means East, PI means South, and 3 * PI / 2 means West. 
	         * 
	         * e.getBearingRadians() returns the angle to the robot, relative to your robot's heading, in radians. -PI < getRobotBearingRadians() <= PI 
	        */
	        double enemyAbsoluteBearing = MyRobot.getHeadingRadians() + e.getBearingRadians();
	        
	        //calculate the absolute position of the enemy on the battlefield
	        double enemyX = MyRobot.getX() + e.getDistance() * Math.sin(enemyAbsoluteBearing);
	        double enemyY = MyRobot.getY() + e.getDistance() * Math.cos(enemyAbsoluteBearing);
	        
	        //get the direction that the enemy robot is facing, in radians.
	        double enemyHeading = e.getHeadingRadians();
	        //calculate the change for the enemy robot
	        double enemyHeadingChange = enemyHeading - MyRobot.oldEnemyHeading;
	        //now the current enemy heading will become the old enemy heading for the next iteration 
	        MyRobot.oldEnemyHeading = enemyHeading;
	        //get the speed of the enemy
	        double enemySpeed = e.getVelocity();
	        
	        //get the dimensions of the battle field
	        double battleFieldHeight = MyRobot.getBattleFieldHeight(), battleFieldWidth = MyRobot.getBattleFieldWidth();
	        double predictedX = enemyX, predictedY = enemyY;
	        //
	        double delta = 0;
	        //in robocode, bullets move with a velocity of: 20 - (3 * bulletPower)
	        double bulletSpeed = 20.0 - 3.0 * bulletPower;
	        /* while the bullet travels the distance from my current position to the enemy robot predict where the enemy will be 
	         * Stop when the bullet has reached the enemy robot or when the enemy is very close to a wall. 
	        */
	        while((++delta) * bulletSpeed < Point2D.Double.distance(myX, myY, predictedX, predictedY)){             
	                //Iteratively predict the position of the enemy robot
	                predictedX += Math.sin(enemyHeading) * enemySpeed;
	                predictedY += Math.cos(enemyHeading) * enemySpeed;
	                enemyHeading += enemyHeadingChange;
	                //if the enemy robot is going to be closer then 18 pixels to one of the four walls
	                if(     predictedX < 18.0 
	                        || predictedY < 18.0
	                        || predictedX > battleFieldWidth - 18.0
	                        || predictedY > battleFieldHeight - 18.0){
	         
	                        /* for X: if 0 <= predictedX <= 18 then choose 18
	                         *                if (battleFieldWidth - 18.0) <= predictedX <= battleFieldWidth then choose (battleFieldWidth - 18.0)
	                        */      
	                        predictedX = Math.min(Math.max(18.0, predictedX), battleFieldWidth - 18.0);     
	                        //same for Y
	                        predictedY = Math.min(Math.max(18.0, predictedY), battleFieldHeight - 18.0);
	                        //terminate the while loop
	                        break;
	                }
	        }
	        
	        //convert from rectangular coordinates (x,y) to polar coordinates (r, theta) and return the angle theta in radians 
	        double theta = Utils.normalAbsoluteAngle(Math.atan2(predictedX - MyRobot.getX(), predictedY - MyRobot.getY()));
	         
	        //point the radar at the enemy robot by turning the radar from its current bearing to the enemy's bearing
	        MyRobot.setTurnRadarRightRadians(Utils.normalRelativeAngle(enemyAbsoluteBearing - MyRobot.getRadarHeadingRadians()));
	        //point the gun at the enemy robot by turning the gun from its current heading to the enemy's predicted heading (theta) 
	        MyRobot.setTurnGunRightRadians(Utils.normalRelativeAngle(theta - MyRobot.getGunHeadingRadians()));
	        //fire the gun
	        if(MyRobot.getGunHeat() == 0){
	        	MyRobot.fire(bulletPower);
	        }
	    }

	 public static void battleEnemy(SimpleDrone MyRobot){
		 while(MyRobot.battleMode){
			 MyRobot.turnRadarLeft(360);
		 }
	 }
}
